package com.jic.manager.impl;

import java.lang.Long;
import java.util.Date;
import java.util.List;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.jic.common.base.vo.AdminOperate;
import com.jic.manager.common.util.BeanUtil;
import com.jic.manager.entity.SysRole;
import com.jic.manager.entity.SysRoleLog;
import com.jic.manager.entity.SysRoleMenuVerify;
import com.jic.manager.enums.RoleTypeEnum;
import com.jic.manager.mapper.SysRoleLogMapper;
import com.jic.manager.mapper.SysRoleMapper;
import com.jic.manager.mapper.SysRoleMenuMapper;
import com.jic.manager.mapper.SysRoleMenuVerifyMapper;
import com.jic.manager.request.SysRoleMenuRequest;
import com.jic.manager.request.SysRoleMenuVerifyRequest;
import com.jic.manager.service.SysRoleMenuVerifyService;
import com.jic.common.base.vo.PageResult;
import com.jic.common.base.vo.Page;

import com.jic.manager.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * 角色授权审核过渡
 * @author : 
 * @email : 
 * @since : 2020-11-08 03:19:42
 * @version : v1.0.0
 */
@Service
public class SysRoleMenuVerifyServiceImpl implements SysRoleMenuVerifyService{

    @Autowired
    private SysRoleMenuVerifyMapper sysRoleMenuVerifyMapper;

    @Autowired
    private SysRoleService sysRoleService;

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;

    @Autowired
    private SysRoleLogMapper sysRoleLogMapper;


    @Transactional(rollbackFor = Exception.class)
    @Override
    public long insert(SysRoleMenuVerifyRequest sysRoleMenuVerifyRequest) {
        AdminOperate adminOperate = sysRoleMenuVerifyRequest.getAdminOperate();
        SysRoleMenuVerify sysRoleMenuVerify = BeanUtil.copy(sysRoleMenuVerifyRequest,SysRoleMenuVerify.class);
        JSONObject map = JSONObject.parseObject(sysRoleMenuVerify.getModifyText());
        SysRole sysRole = new SysRole();
        sysRole.setRoleName(sysRoleMenuVerify.getRoleName());
        sysRole.setType(map.getString("type"));
        if("0".equals(map.getString("id"))){
            sysRole.setCreator(adminOperate.getOperator());
            sysRole.setCreatorName(adminOperate.getOperateName());
            sysRole.setDeptId(adminOperate.getDeptId());
            sysRoleMapper.insert(sysRole);
        }else{
            sysRole.setUpdater(adminOperate.getOperator());
            sysRole.setUpdaterName(adminOperate.getOperateName());
            sysRole.setId(map.getLong("id"));
            sysRoleMapper.update(sysRole);

        }
        sysRoleMenuVerify.setStatus("1");
        sysRoleMenuVerify.setRoleid(sysRole.getId());
        sysRoleMenuVerify.setCreator(adminOperate.getOperator());
        sysRoleMenuVerify.setCreatorName(adminOperate.getOperateName());
        sysRoleMenuVerify.setDeptId(adminOperate.getDeptId());
        long flag = sysRoleMenuVerifyMapper.insert(sysRoleMenuVerify);
        return flag;
    }

    @Override
    public long delete(Long id) {
        return sysRoleMenuVerifyMapper.delete(id);
    }

    @Override
    public long update(SysRoleMenuVerify sysRoleMenuVerify) {
        return sysRoleMenuVerifyMapper.update(sysRoleMenuVerify);
    }

    @Override
    public SysRoleMenuVerify queryByPrimaryKey(Long id){
        return sysRoleMenuVerifyMapper.queryByPrimaryKey(id);
    }

    @Override
    public List<SysRoleMenuVerify> query(SysRoleMenuVerify sysRoleMenuVerify){
        return sysRoleMenuVerifyMapper.query(sysRoleMenuVerify);
    }

    @Override
    public PageResult<SysRoleMenuVerify> queryPage(SysRoleMenuVerifyRequest sysRoleMenuVerifyRequest, Page pageQuery) {
        //计算下标
        int startIndex = (pageQuery.getStart() - 1) * pageQuery.getLimit();
        List<SysRoleMenuVerify> list = sysRoleMenuVerifyMapper.queryPage(startIndex, pageQuery.getLimit(), sysRoleMenuVerifyRequest);
        long count = sysRoleMenuVerifyMapper.queryPageCount(sysRoleMenuVerifyRequest);
        PageResult pageResult = new PageResult();
        pageResult.setRows(list);
        pageResult.setTotal(count);
        return pageResult;
    }

    @Override
    public SysRoleMenuVerify getSysRoleMenuVerifyByRoleId(Long roleId) {
        return sysRoleMenuVerifyMapper.getSysRoleMenuVerifyByRoleId(roleId);
    }
}